﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Web.Mvc;
using System.Web;
namespace Services
{
    public partial class SearchService
    {

        readonly IUserEntity entity;
        readonly IProductEntity Productentity;
        readonly ICategoryEntity Categoryenity;
        readonly ICategoryArticleEntity CategoryArticleentity;
        readonly IArticleEntity Articelenity;
        public SearchService(IUserEntity entity, ICategoryEntity category, IArticleEntity articel, ICategoryArticleEntity _categoryArticle, IProductEntity _product)
       {
           this.entity = entity;
           this.Categoryenity = category;
           this.CategoryArticleentity = _categoryArticle;
           this.Productentity = _product;
           this.Articelenity = articel;
       }

        public IList<User> SearchUser(string name, string username, string email, string groupid)
        {
            try {
          
                if(!string.IsNullOrEmpty(name))
                    name = " and Name like N'%" + name + "%'";
                if(!string.IsNullOrEmpty(username))
                    username = " and UserName like N'%" + username + "%'";
                if(!string.IsNullOrEmpty(email))
                    email = " and Email like N'%" + email + "%'";           
                return entity.GetByCusTomSQL(string.Format(SQLCommand.SearchUser,groupid,name,username,email)).ToList();
            }
            catch { return null; }
        }

        public IList<Search> SearchAll(string value)
        {
            try
            {
                IList<Search> lst = new List<Search>();

                var product = new List<Product>();

                product = Productentity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Product.ToString()).ToList();

                if (product != null)
                {
                    foreach (var item in product)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Sản phẩm";
                        s.Image = "/Media/Product/" + item.Image;
                        s.Link = "/product/details/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.ShortDescription;
                        lst.Add(s);
                    }
                }

                var article = new List<Article>();

                article = Articelenity.GetMany(c => StringHelper.RemoveVietNamString(c.Title.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Article.ToString()).ToList();

                if (article != null)
                {
                    foreach (var item in article)
                    {
                        Search s = new Search();
                        s.Name = item.Title;
                        s.Type = "Bài viết";
                        s.Image = "/Media/Article/" + item.Image;
                        s.Link = "/article/details/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.ShortDescription;
                        lst.Add(s);
                    }
                }

                var category = new List<Category>();

                category = Categoryenity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Category.ToString()).ToList();

                if (category != null)
                {
                    foreach (var item in category)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Danh mục sản phẩm";
                        s.Image = "/Media/Category/" + item.Image;
                        s.Link = "/product-list/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.Note;
                        lst.Add(s);
                    }
                }
                var categoryarticle = new List<CategoryArticle>();

                categoryarticle = CategoryArticleentity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.CategoryArticle.ToString()).ToList();

                if (categoryarticle != null)
                {
                    foreach (var item in categoryarticle)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Danh mục bài viết";
                        s.Image = "/Media/Category/" + item.Image;
                        s.Link = "/articles-list/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.Description;
                        lst.Add(s);
                    }
                }
                return lst;
            }
            catch
            {
                return null;
            }
        }

        public IList<Search> SearchByLimit(string value, int limit)
        {
            try
            {
                IList<Search> lst = new List<Search>();

                var product = new List<Product>();

                product = Productentity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Product.ToString()).Take(limit).ToList();

                if (product != null)
                {
                    foreach (var item in product)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Sản phẩm";
                        s.Image = "/Media/Product/" + item.Image;
                        s.Link = "/product-detail/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.ShortDescription;
                        lst.Add(s);
                    }
                }

                var category = new List<Category>();

                category = Categoryenity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Category.ToString()).Take(limit).ToList();

                if (category != null && product.Count() < limit)
                {
                    foreach (var item in category)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Danh mục sản phẩm";
                        s.Image = "/Media/Category/" + item.Image;
                        s.Link = "/product-list/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.Note;
                        lst.Add(s);
                    }
                }

                var article = new List<Article>();

                article = Articelenity.GetMany(c => StringHelper.RemoveVietNamString(c.Title.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.Article.ToString()).Take(limit).ToList();

                if (article != null && category.Count()< limit)
                {
                    foreach (var item in article)
                    {
                        Search s = new Search();
                        s.Name = item.Title;
                        s.Type = "Bài viết";
                        s.Image = "/Media/Article/" + item.Image;
                        s.Link = "/article_details/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.ShortDescription;
                        lst.Add(s);
                    }
                }

                var categoryarticle = new List<CategoryArticle>();

                categoryarticle = CategoryArticleentity.GetMany(c => StringHelper.RemoveVietNamString(c.Name.ToLower()).Contains(StringHelper.RemoveVietNamString(value)), Table.CategoryArticle.ToString()).Take(limit).ToList();

                if (categoryarticle != null && article.Count() < limit)
                {
                    foreach (var item in categoryarticle)
                    {
                        Search s = new Search();
                        s.Name = item.Name;
                        s.Type = "Danh mục bài viết";
                        s.Image = "/Media/Category/" + item.Image;
                        s.Link = "/articles-list/" + item.ID + "/" + item.Link + ".html";
                        s.Description = item.Description;
                        lst.Add(s);
                    }
                }
                return lst;
            }
            catch
            {
                return null;
            }
        }

        public List<SelectListItem> GetSelectListCategory()
        {
            var lst = new List<SelectListItem>();

            var listcate = Categoryenity.GetMany(x => x.Active == true && x.ParentID == 0, Table.Category.ToString());
            lst.Add(new SelectListItem
            {
                Text = "Tất cả",
                Value = "0"
            });
            foreach (var c in listcate)
            {
                var item = new SelectListItem();
                item.Text = c.Name;
                item.Value = c.ID.ToString();
                lst.Add(item);
            }
            return lst;
        }
    }
}
